package com.qf.cloud2204.microa.controller;

import cn.hutool.http.HttpUtil;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/a")
@Slf4j
public class TestController {
    @Value("${server.port}")
    Integer port;
    @Autowired
    EurekaClient eurekaClient;


    @GetMapping("/hello")
    public String hello(){
        //模拟执行时长为10s
        //Thread.sleep(10000);
//        try {
//            TimeUnit.SECONDS.sleep(10);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        log.debug("开始处理请求/hello");
        return "hello spring cloud"+port;

    }


    @PostMapping("/token")
    public String token(String number){
        //调用微服务token的接口: get请求 /token/gentoken

        //根据服务的名字，获取服务的ip地址和端口号
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROTOKEN", false);
        String homePageUrl = instanceInfo.getHomePageUrl();
        String hostName = instanceInfo.getHostName();
        String ipAddr = instanceInfo.getIPAddr();
        int port = instanceInfo.getPort();
        //组织请求的url
        String url = homePageUrl+"token/gentoken";
        HashMap<String, Object> paramMap  = new HashMap<>();
        paramMap.put("number",number);
        //发送post请求到token模块genToken接口并返回数据
        String result = HttpUtil.post(url, paramMap);

        return result;
    }

    @PostMapping("/verify")
    public String verify(String token){
        //调用微服务token的接口: get请求 /token/gentoken

        //根据服务的名字，获取服务的ip地址和端口号
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROTOKEN", false);
        String homePageUrl = instanceInfo.getHomePageUrl();
        String hostName = instanceInfo.getHostName();
        String ipAddr = instanceInfo.getIPAddr();
        int port = instanceInfo.getPort();
        //组织请求的url
        String url = homePageUrl+"token/verify";
        HashMap<String, Object> paramMap  = new HashMap<>();
        paramMap.put("token",token);
        //发送post请求到token模块genToken接口并返回数据
        String result = HttpUtil.post(url, paramMap);

        return result;
    }



}
